package org.walkerljl.boss.service.sys.impl;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.walkerljl.boss.dao.sys.AppConfigDao;
import org.walkerljl.boss.domain.sys.AppConfig;
import org.walkerljl.boss.support.dao.BaseDao;
import org.walkerljl.boss.support.enums.Status;
import org.walkerljl.boss.support.service.impl.JqueryDatatableBaseServiceImpl;
import org.walkerljl.boss.service.sys.AppConfigService;
import org.walkerljl.commons.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;

/**
 * AppConfigServiceImpl
 *
 * @author lijunlin
 */
@Service("appConfigService")
public class AppConfigServiceImpl extends JqueryDatatableBaseServiceImpl<Long, AppConfig> implements AppConfigService {

    @Resource
    private AppConfigDao appConfigDao;

    @Override
    public BaseDao<Long, AppConfig> getDao() {
        return appConfigDao;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void testTransaction() {
        appConfigDao.updateStatusByKey2("system.upgrading");

        for (int i = 1; i <= 2; i++) {
            AppConfig sysConfig = new AppConfig();
            sysConfig.setName(i + "");
            sysConfig.setKey(i + "");
            sysConfig.setValue(i + "");
            sysConfig.setRemark(i + "");
            sysConfig.setStatus(1);
            sysConfig.setCreator("lijunlin");
            sysConfig.setCreated(new Date());
            sysConfig.setModifier(sysConfig.getModifier());
            sysConfig.setModified(sysConfig.getCreated());
            if (i == 2) {
                throw new RuntimeException("testTransaction");
            }
        }
    }

    @Override
    public AppConfig getAppConfigByKey(Long appId, String appConfigKey) {
        if (StringUtils.isBlank(appConfigKey)) {
            return null;
        }
        AppConfig condition = new AppConfig();
        condition.setAppId(appId);
        condition.setKey(appConfigKey.trim().toLowerCase());
        condition.setStatus(Status.ENABLED.getValue());
        return selectOne(condition);
    }
}
